﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using RemotingFramework = DreamCube.Framework.Utilities.Remoting;
using SisenMESS_SinfoPlugIn.Remoting;

namespace Server
{
    class Program
    {
        private static Remoting.TestClass testClass;
        private static Remoting.ActiveXDemo activeXDemo;
        private static SystemCommand sysCommand;
        private static SystemConnection sysConnection;

        static void Main(string[] args)
        {
            Console.WriteLine("初始化服务器端....");
            RemotingFramework.Server.Instance.InitialServer(9002, "testRemoting");
            sysCommand = new SystemCommand();
            RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(sysCommand);

            sysConnection = new SystemConnection();
            sysConnection.ClientConnect += new EventHandler(sysConnection_ClientConnect);
            RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(sysConnection);

            Console.ReadKey();
        }

        static void sysConnection_ClientConnect(object sender, EventArgs e)
        {
            Console.WriteLine("客户端连接上了");

            Action a = new Action(() =>
            {
                Thread.Sleep(3000);
                for (Int32 i = 0; i < 100; i++)
                {
                    Console.WriteLine(String.Format("向客户发送消息：{0}", i));
                    Thread.Sleep(3000);
                    sysCommand.FireSystemCommandEvent2(i.ToString());
                }
            });
            a.BeginInvoke(null, null);
        }

        static void TestRemotingActiveX()
        {
            Console.WriteLine("初始化服务器端....");
            RemotingFramework.Server.Instance.InitialServer(9002, "testRemoting");
            activeXDemo = new Remoting.ActiveXDemo();
            RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(activeXDemo);

            Action a = new Action(() =>
            {
                Thread.Sleep(3000);
                for (Int32 i = 0; i < 100; i++)
                {
                    Console.WriteLine(String.Format("向客户发送消息：{0}", i));
                    Thread.Sleep(3000);
                    activeXDemo.FireShowMessage(i.ToString());
                }
            });
            a.BeginInvoke(null, null);

            Console.ReadKey();
        }

        static void TestConsoleToConsole()
        {
            Console.WriteLine("初始化服务器端....");
            RemotingFramework.Server.Instance.InitialServer(9002, "testRemoting");
            testClass = new Remoting.TestClass();
            Console.WriteLine("成功注册远程对象！");
            RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(testClass);

            Action a = new Action(() =>
            {
                Thread.Sleep(3000);
                for (Int32 i = 0; i < 100; i++)
                {
                    Console.WriteLine(String.Format("向客户发送消息：{0}", i));
                    Thread.Sleep(1000);
                    testClass.FireGetMsgEvent(i.ToString());
                }
            });
            a.BeginInvoke(null, null);

            Console.ReadKey();
        }
    }
}
